<template>
	<view>
		<!-- 搜索组件 -->
		<view class="search">
			<my-search @click='gotoSearch'></my-search>
		</view>
		<view class="scroll-container">
			<!-- 左侧一级分类 -->
			<scroll-view class="left-scroll" scroll-y="true" :style="{height:availableHeight + 'px'}">
				<block v-for="(item,index) in firstCateList" :key="index">
					<view :class="['scroll-item',index===firstActiceIndex?'active':'']" @click="selectFirstCte(index)">{{ item.cat_name }}</view>
				</block>
			</scroll-view>
			<!-- 右侧二级分类 -->
			<scroll-view scroll-y="true" :style="{height:availableHeight+'px'}" :scroll-top="scrollTop">
				<view class="secondCate" v-for="(item,index) in secondCateList" :key="index">
					<!-- 标题 -->
					<view class="secondCateTitle">
						/ {{ item.cat_name }} /
					</view>
					<!-- 三级分类 -->
					<view class="thirdCate">
						<view class="thirdCateItem" v-for="(item1,index1) in item.children" :key="index1" @click="gotoGoodsList(item1.cat_id)">
							<image :src="item1.cat_icon"></image>
							<text>{{item1.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badegeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badegeMix],
		data() {
			return {
				// 屏幕的可用高度
				availableHeight: 0,
				// 一级分类列表
				firstCateList: [],
				// 二级分类列表
				secondCateList: [],
				// 一级分类选中项索引
				firstActiceIndex: 0,
				// 滚动条位置
				scrollTop: 0
			};
		},
		onLoad() {
			// 获取屏幕的可用高度
			this.availableHeight = uni.getSystemInfoSync().windowHeight - 50
			// 获取一级分类列表
			this.getFirstCateList()
		},
		methods: {
			// 获取一级分类列表请求
			async getFirstCateList() {
				const res = await uni.$http.get('/api/public/v1/categories')
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 一级分类
				this.firstCateList = res.data.message
				// 二级分类
				this.secondCateList = this.firstCateList[0].children
			},
			// 切换一级分类事件
			selectFirstCte(index) {
				// this.scrollTop = 1
				this.firstActiceIndex = index
				this.secondCateList = this.firstCateList[index].children
				this.scrollTop = this.scrollTop === 0 ? 0.01 : 0
			},
			// 点击三级分类跳转
			gotoGoodsList(id) {
				console.log(id)
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + id
				})
			},
			// 点击搜索框跳转
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}

		}
	}
</script>

<style lang="scss">
	.scroll-container {
		display: flex;
	}

	.left-scroll {
		width: 120px;
	}

	.scroll-item {
		width: 120px;
		line-height: 60px;
		background-color: #f7f7f7;
		text-align: center;
		font-size: 12px;

		&.active {
			background-color: #ffffff;
			position: relative;

			&::before {
				content: ' ';
				display: block;
				width: 3px;
				height: 35px;
				background-color: #c00000;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}

	.secondCateTitle {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	.thirdCate {
		display: flex;
		flex-wrap: wrap;

		.thirdCateItem {
			width: 33.33%;
			margin-bottom: 10px;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 60px;
				height: 60px;
			}

			text {
				font-size: 12px;
			}
		}
	}

	.search {
		position: sticky;
		top: 0;
		z-index: 999;
	}
</style>
